package Facade;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import Service.ServiceFactory;
import VOs.CompanyVO;
import VOs.MarketVO;
import VOs.QuoteVO;
import VOs.StockVO;
import VOs.UsuarioVO;

public class MainFacade {
	private EntityManager emR;
	private EntityTransaction tx;
	private EntityManager emU;

	public MainFacade() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("modeloPU");
		emR = emf.createEntityManager();
		EntityManagerFactory emfU = Persistence.createEntityManagerFactory("usuariosPU");
		emU = emfU.createEntityManager();
	}

	// Markets
	public void createMarket(MarketVO newOne) {
		this.openTransaction(emR);
		ServiceFactory.getMarketService().create(newOne, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	public List<MarketVO> readMarket(MarketVO marketVo) {
		this.openTransaction(emR);
		List<MarketVO> markets = ServiceFactory.getMarketService().readMarkets(emR);
		tx.commit();
		this.closeTransaction(emR);
		return markets;
	}

	public void updateMarket(MarketVO oldOne, MarketVO newOne) {
		this.openTransaction(emR);
		ServiceFactory.getMarketService().update(oldOne, newOne, emR);
		tx.commit();
		this.closeTransaction(emR);

	}

	public void deleteMarket(MarketVO marketVo) {
		this.openTransaction(emR);
		ServiceFactory.getMarketService().delete(marketVo, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	// Companies
	public void createCompany(CompanyVO companyVo) {
		this.openTransaction(emR);
		ServiceFactory.getCompanyService().create(companyVo, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	public List<CompanyVO> readCompany(MarketVO marketVo) {
		this.openTransaction(emR);
		List<CompanyVO> companies = ServiceFactory.getCompanyService().readCompanies(marketVo, emR);
		tx.commit();
		this.closeTransaction(emR);
		return companies;

	}

	public void updateCompany(CompanyVO oldOne, CompanyVO newOne) {
		this.openTransaction(emR);
		ServiceFactory.getCompanyService().update(oldOne, newOne, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	public void deleteCompany(CompanyVO companyVo) {
		this.openTransaction(emR);
		ServiceFactory.getCompanyService().delete(companyVo, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	// Stocks
	public void createStock(StockVO stockVo) {
		this.openTransaction(emR);
		ServiceFactory.getStockService().create(stockVo, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	public List<StockVO> readStock(CompanyVO companyVo) {
		this.openTransaction(emR);
		List<StockVO> stocks = ServiceFactory.getStockService().read(companyVo, emR);
		tx.commit();
		this.closeTransaction(emR);
		return stocks;

	}

	public void updateStock(StockVO oldOne, StockVO newOne) {
		this.openTransaction(emR);
		ServiceFactory.getStockService().update(oldOne, newOne, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	public void deleteStock(StockVO stockVo) {
		this.openTransaction(emR);
		ServiceFactory.getStockService().delete(stockVo, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	// Quote
	public void createQuote(QuoteVO quoteVo) {
		this.openTransaction(emR);
		ServiceFactory.getQuoteService().create(quoteVo, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	public List<QuoteVO> readQuote(StockVO quoteVo) {
		this.openTransaction(emR);
		List<QuoteVO> quotes = ServiceFactory.getQuoteService().readQuoter(quoteVo, emR);
		tx.commit();
		this.closeTransaction(emR);
		return quotes;

	}

	public void updateQuote(QuoteVO oldOne, QuoteVO newOne) {
		this.openTransaction(emR);
		ServiceFactory.getQuoteService().update(oldOne, newOne, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	public void deleteQuote(QuoteVO quoteVo) {
		this.openTransaction(emR);
		ServiceFactory.getQuoteService().delete(quoteVo, emR);
		tx.commit();
		this.closeTransaction(emR);
	}

	private void openTransaction(EntityManager em) {
		tx = em.getTransaction();
		tx.begin();
	}

	private void closeTransaction(EntityManager em) {
		tx = null;
		em.clear();
		em.close();
	}
	
	public UsuarioVO login(UsuarioVO usuarioVo){
		this.openTransaction(emU);
		UsuarioVO users = ServiceFactory.getUserService().login(usuarioVo, emR);
		tx.commit();
		this.closeTransaction(emU);
		return users;
	}
}
